#pragma once

#include "Protocol.hpp"
#include <functional>

using service_t = std::function<Response(const Request &req)>;

static Response calculator(const Request &req)
{
    Response resp = {0, 0};
    switch (req._op)
    {
    case '+':
        resp._result = req._x + req._y;
        break;
    case '-':
        resp._result = req._x - req._y;
        break;
    case '*':
        resp._result = req._x * req._y;
        break;
    case '/':
    { // _x_ / _y_
        if (req._y == 0)
            resp._exitCode = -1; // -1. 除0
        else
            resp._result = req._x / req._y;
    }
    break;
    case '%':
    { // _x_ / _y_
        if (req._y == 0)
            resp._exitCode = -2; // -2. 模0
        else
            resp._result = req._x % req._y;
    }
    break;
    default:
        resp._exitCode = -3; // -3: 非法操作符
        break;
    }

    return resp;
}
